package com.realgodo.site.service.space;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.hibernate.DefaultEntityManager;

import com.realgodo.site.entity.space.Space;

@Service
@Transactional
public class SpaceManager extends DefaultEntityManager<Space, Long> {

	@Autowired
	protected SessionFactory sessionFactory;

	@SuppressWarnings("unchecked")
	public Space getSpaceByUserId(Long userId) {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				Space.class);
		c.add(Restrictions.eq("user.id", userId));
		List<Space> spaceList = c.list();
		return spaceList == null || spaceList.size() == 0 ? null : spaceList
				.get(0);
	}

	@SuppressWarnings("unchecked")
	public Space getSpaceByDomain(String domain) {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				Space.class);
		c.add(Restrictions.eq("domain", domain));
		List<Space> spaceList = c.list();
		return spaceList == null || spaceList.size() == 0 ? null : spaceList
				.get(0);
	}

}
